//
//  Models.swift
//  CellAutolayoutHeight
//
//  Created by zk_01 on 2022/3/29.
//

import Foundation
import UIKit
import SwiftUI

enum CellType:String,Codable {
    case text           = "text"
    case image          = "image"
    case hotTopics      = "hotTopics"
}

class DataModel:Codable {
    
    /// 热门话题
    class HotTopics:Codable {
        /// 标题
        var title:String
        
        /// 话题描述
        var des:String?
    }
    
    var id:String
    /// 发布者-用户名
    var name:String
    
    /// 发布者肖像
    var portraitUrl:String
    
    /// 内容
    var content:String
    
    /// 时间
    var timeStr:String
    
    /// 图片数组
    var imgsUrl:[String]
    
    /// 话题数组
    var hotTopics:[HotTopics]
        
    /// 是否点赞
    var isLike:Bool
    
    /// 评论数量
    var commentCount:String
    
    /// cell高度缓存
    var cellHeight:CGFloat = 0.0
    
    /// cell类型
    var cellType:CellType = .text
    
    /// 文字内容长度
    var textLineCount:UInt = 9
    
    var offset:CGPoint = .zero
    
    enum CodingKeys : String, CodingKey{
        case id
        case name
        case portraitUrl
        case content
        case imgsUrl
        case hotTopics
        case timeStr            = "time"
        case isLike
        case commentCount
    }
    
}

class DataPrepare{
    
    static let shared = DataPrepare()
    
    var dataSourece = [DataModel]()
    
    func loadData()->[DataModel]{
        self.dataSourece.removeAll()

        for v in 1...30 {
            for (indx,model) in self.getDataModel(type: [DataModel].self)!.enumerated(){
                let length = Int(arc4random()) % model.content.count
                let isShowFold = Int(arc4random()) % 2
                model.content = String(model.content.prefix((isShowFold != 0) ? length : 150))
                model.content = "\(model.content)123-123-123-123-123"
                model.id = String(v) + "-" + String(indx)
                
                //话题数
                let startIndex = Int(arc4random()) % model.hotTopics.count
                let endIndex = Int(arc4random()) % model.hotTopics.count
                if startIndex > endIndex {
                    model.hotTopics = model.hotTopics[endIndex...startIndex].map({ $0 })
                }else if startIndex < endIndex{
                    model.hotTopics = model.hotTopics[startIndex...endIndex].map({ $0 })
                }else{
                    model.hotTopics = [model.hotTopics[startIndex]]
                }
                
                //是否显示话题
                let isEnabelHotTopics = Int(arc4random()) % 3 == 0
                if isEnabelHotTopics {
                    model.hotTopics = [DataModel.HotTopics]()
                }
                
                //图片数量
                let count =  Int(arc4random()) % 4

                var imgUrls = [String]()
                for _ in 0...count {
                    let index = Int(arc4random()) % model.imgsUrl.count
                    imgUrls.append(model.imgsUrl[index])
                }
                
                //是否为图片类型
                let isImage = Int(arc4random()) % 3 != 1
                
                //图片类型
                if !imgUrls.isEmpty && isImage {
                    model.cellType = .image
                }
                model.imgsUrl = imgUrls
                
                
                self.dataSourece.append(model)
            }
        }
        return  self.dataSourece
    }
    
    func getDataModel<T:Codable>(type:T.Type)->T?{
        let dataURL = URL(fileURLWithPath: Bundle.main.path(forResource:"data", ofType:".json")!)
        let datas = try! Data(contentsOf: dataURL)
        //解析器
        let decoder = JSONDecoder.init()
        //解析出对应的模型
        do {
            let parseRes = try decoder.decode(type, from: datas)
            return parseRes
        } catch  let err as NSError {
  
           fatalError("解析出错:\(err)")
        }
    }
}
